package pic;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import org.junit.jupiter.api.Test;

import javax.imageio.ImageIO;
import java.io.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhan
 * @since 2020-12-17 15:01
 */
public class compress {
    @Test
    public void test() {
        try {
            InputStream in = new FileInputStream("C:\\Users\\zhan\\Desktop\\yy_xy_jbxx.xlsx.xls");
            in.mark(10);
            if (in.markSupported()) {
                System.out.println("支持reset");
            }
            int available = in.available();
            System.out.println("------------------->"+available);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testCountDownLatch() throws InterruptedException {
        CountDownLatch clt = new CountDownLatch(1);
        for (int i = 0; i <= 5; i++) {
            new Thread(() -> {
                try {
                    clt.await();
                    String parter = "【" + Thread.currentThread().getName() + "】";
                    System.out.println(parter + "开始执行……");
                } catch (Exception e) {
                    //
                }
            }).start();
        }
        Thread.sleep(2000);
        clt.countDown();
    }
    @Test
    public void testCountDownLatch2() throws InterruptedException {
        CountDownLatch clt = new CountDownLatch(5);
        for (int i = 0; i <= 5; i++) {
            final int index = i;
            new Thread(() -> {
                try {
                    Thread.sleep(1000 + ThreadLocalRandom.current().nextInt(1000));
                    System.out.println("finish" + index + Thread.currentThread().getName());
                    clt.countDown();
                } catch (Exception e) {
                    //
                }
            }).start();
        }
        clt.await();
        System.out.println("主线程:在所有任务运行完成后，进行结果汇总");
    }
    private static int m = 0;
    @Test
    public void testCtl() throws InterruptedException {
        CountDownLatch cdl = new CountDownLatch(2);

        Thread t1 = new Thread(() -> {
            for (int j = 0; j < 5000; j++) {
                m++;
            }
            cdl.countDown();
        });
        Thread t2 = new Thread(() -> {
            for (int j = 0; j < 5000; j++) {
                m++;
            }
            cdl.countDown();
        });
        t1.start();
        t2.start();

        cdl.await();
        System.out.println("result=" + m);
    }

    @Test
    public void testAtomicInteger() throws InterruptedException {
        CountDownLatch ctl = new CountDownLatch(2);
        AtomicInteger a = new AtomicInteger(0);
        int order = a.addAndGet(5);
        System.out.println("---======="+order);
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                a.incrementAndGet();
            }
            ctl.countDown();
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                a.incrementAndGet();
            }
            ctl.countDown();
        });
        thread1.start();
        thread2.start();
        ctl.await();
        System.out.println("result=" + a.get());
    }
    @Test
    public void 压缩() throws IOException {
//        ImgUtil.scale(
//                FileUtil.file("E:\\壁纸\\微信图片_20201016165426.jpg"),
//                FileUtil.file("C:\\Users\\zhan\\Desktop\\222.jpg"),
//                0.5f
//        );
        System.out.println("成功！");
//        ImgUtil.scale();
        BufferedInputStream inputStream = FileUtil.getInputStream("E:\\壁纸\\微信图片_20201016165426.jpg");
//        BufferedOutputStream outputStream = FileUtil.getOutputStream("C:\\Users\\zhan\\Desktop\\333.jpg");
//        BufferedOutputStream out = FileUtil.load()

//        inputStream.mark(1);
//        if (inputStream.markSupported()) {
//            System.out.println("123456");
//        }
//        ImgUtil.scale(inputStream,outputStream,0.5f);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream stream = new FileInputStream("C:\\Users\\zhan\\Desktop\\西安地铁1.jpg");
//        ImageIO.write(ImageIO.read(stream), "jpg", out);
        ImgUtil.scale(ImageIO.read(stream),out,0.5f);
        System.out.println(out.size());
        System.out.println(out.toString());
        System.out.println(out.toString("utf-8"));
    }
}
